package com.fengye.algorithms.leecode;

import java.util.Arrays;

/**
 * @Author fengye
 * @Date 2023/4/11 9:07
 **/
public class FindMedianSortedArrays04 {
    public static void main(String[] args) {
        int[] nums1 = {1,3};
        int[] nums2 = {2};
        System.out.println(new FindMedianSortedArrays04().findMedianSortedArrays(nums1, nums2));
    }
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n = nums1.length;
        int m = nums2.length;

        int l = n + m;
        int[] nums = new int[l];
        if(n>0) {
            System.arraycopy(nums1, 0, nums, 0, n);
        }
        if(m>0) {
            System.arraycopy(nums2, 0, nums, n, m);
        }

        if(l==1) {
            return nums[0];
        }

        Arrays.sort(nums);

        if((l & 1) == 0) {
            return (double)(nums[l/2 -1] + nums[l/2])/2;
        }

        return (double) (nums[(l-1)/2]);
    }
}
